﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Useful.GUI
{
    public class CollapsedDataGridViewRowHeaderCell : DataGridViewRowHeaderCell
    {
        private List<DataGridViewRow> group = null;
        private CollapsedDataGridViewControllerType rowType = CollapsedDataGridViewControllerType.Simple;

        private readonly Pen pen = new Pen(Color.Black, 2);
        private readonly Pen pen2 = new Pen(Color.Black, 1);

        public event EventHandler BeforeCollapsing;

        public CollapsedDataGridViewControllerType RowType
        {
            get
            {
                return rowType;
            }
        }

        public bool IsGroupHeader
        {
            get
            {
                return group != null;
            }
        }

        public CollapsedDataGridViewRowHeaderCell(CollapsedDataGridViewControllerType value)
            : base()
        {
            rowType = value;
        }

        public CollapsedDataGridViewRowHeaderCell(CollapsedDataGridViewControllerType value, List<DataGridViewRow> group)
            : base()
        {
            this.group = group;
            rowType = value;
        }

        public CollapsedDataGridViewRowHeaderCell(CollapsedDataGridViewControllerType value, List<DataGridViewRow> group, EventHandler BeforeCollapsing)
            : base()
        {
            this.group = group;
            rowType = value;
            this.BeforeCollapsing = BeforeCollapsing;
        }

        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            if (group != null)
            {
                if (BeforeCollapsing != null)
                    BeforeCollapsing(this, EventArgs.Empty);

                CollapseGroup(true);
            }

            base.OnMouseClick(e);
        }

        public void CollapseGroup(bool needSuspend)
        {
            if (needSuspend)
                base.DataGridView.SuspendLayout();

            for (int i = 1; i < group.Count; i++)
                group[i].Visible = !group[i].Visible;

            switch (rowType)
            {
                case CollapsedDataGridViewControllerType.Simple:
                    break;
                case CollapsedDataGridViewControllerType.GroupRowItem:
                    break;
                case CollapsedDataGridViewControllerType.LastGroupItem:
                    break;
                case CollapsedDataGridViewControllerType.GroupHeaderPlus:
                    rowType = CollapsedDataGridViewControllerType.GroupHeaderMinus;
                    break;
                case CollapsedDataGridViewControllerType.GroupHeaderMinus:
                    rowType = CollapsedDataGridViewControllerType.GroupHeaderPlus;
                    break;
            }

            if (needSuspend)
                base.DataGridView.ResumeLayout();
        }

        public void CollapseGroup(bool needSuspend, bool isVisible)
        {
            if (needSuspend)
                base.DataGridView.SuspendLayout();

            for (int i = 1; i < group.Count; i++)
            {
                //((CollapsedDataGridView)(base.DataGridView)).GoToFirstRowInGroup(group[i]);
                group[i].Visible = isVisible;
            }

            switch (rowType)
            {
                case CollapsedDataGridViewControllerType.Simple:
                    break;
                case CollapsedDataGridViewControllerType.GroupRowItem:
                    break;
                case CollapsedDataGridViewControllerType.LastGroupItem:
                    break;
                case CollapsedDataGridViewControllerType.GroupHeaderPlus:
                    if (isVisible)
                        rowType = CollapsedDataGridViewControllerType.GroupHeaderMinus;
                    break;
                case CollapsedDataGridViewControllerType.GroupHeaderMinus:
                    if (!isVisible)
                        rowType = CollapsedDataGridViewControllerType.GroupHeaderPlus;
                    break;
            }

            if (needSuspend)
                base.DataGridView.ResumeLayout();
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts & ~DataGridViewPaintParts.ContentBackground);

            float x = cellBounds.Left + 7;
            float y = cellBounds.Top;
            float h = cellBounds.Height;

            switch (rowType)
            {
                case CollapsedDataGridViewControllerType.Simple:
                    break;
                case CollapsedDataGridViewControllerType.GroupRowItem:
                    graphics.DrawLine(pen, x, y, x, y + h);
                    graphics.DrawLine(pen, x + 14, y + h / 2, x + 16, y + h / 2);
                    break;
                case CollapsedDataGridViewControllerType.LastGroupItem:
                    graphics.DrawLine(pen, x, y + 0, x, y + h); // Вертикальная
                    graphics.DrawLine(pen, x + 14, y + h / 2, x + 16, y + h / 2); // Точка
                    graphics.DrawLine(pen, x - 1, y - 1 + h, x + 7, y - 1 + h); // Горизонтальная
                    break;
                case CollapsedDataGridViewControllerType.GroupHeaderPlus:
                    graphics.DrawRectangle(pen2, x - 4, y + h - 14, 13, 13);
                    graphics.DrawLine(pen, x, y + h - 7, x + 6, y + h - 7);

                    graphics.DrawLine(pen, x + 3, y + h - 10, x + 3, y + h - 4);
                    break;
                case CollapsedDataGridViewControllerType.GroupHeaderMinus:
                    graphics.DrawRectangle(pen2, x - 4, y + h - 14, 13, 13);
                    graphics.DrawLine(pen, x, y + h - 7, x + 6, y + h - 7);
                    break;
            }
        }
    }
}